package gw.comp.tabbedpane;

import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author jcastillo
 */
public class TabbedPanePrincipal extends JTabbedPane {

    private ArrayList<String> arrTitulosPaneles;

    public TabbedPanePrincipal() {
        arrTitulosPaneles = new ArrayList<String>();
    }

    public void agregarTab(JPanel pnlTab, String titulo) {
        int numPaneles = this.getTabCount();
        int index = -1;
        for (int i = 0; i < numPaneles; i++) {
            if (titulo.equals(this.getTitleAt(i)) && existeTab(this.getTitleAt(i))) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            this.setSelectedIndex(index);
            pnlTab.removeAll();
            System.runFinalization();
            System.gc();
        } else {
            ButtonTabComponent btnX = new ButtonTabComponent(this);
            this.addTab(titulo, null, pnlTab);
            this.setTabComponentAt(numPaneles, btnX);
            this.setSelectedIndex(numPaneles);
            arrTitulosPaneles.add(titulo);
        }

    }

    private boolean existeTab(String titulo) {
        for (String arrTitulo : arrTitulosPaneles) {
            if (arrTitulo.equals(titulo)) {
                return true;
            }
        }
        return false;
    }
}
